/***********************************************************************
 * Module:  NetworkUtility.java
 * Author:  Administrator
 * Purpose: Defines the Class NetworkUtility
 ***********************************************************************/

package vn.com.vega.music.network;

import vn.com.vega.chacha.v2.receiver.NetworkStatusReceiver;
import android.graphics.Bitmap;

public class NetworkUtility {
	private static boolean isViettel = false;

	public static void setIsViettel(boolean value) {
		synchronized (NetworkUtility.class) {
			isViettel = value;
		}
	}

	public static boolean getIsViettel() {
		synchronized (NetworkUtility.class) {
			return isViettel;
		}
	}

	/*
	 * Note: When you want to cancel request. You have to call: cd.close() to
	 * cancel listener call back.
	 */
	public static NetworkRequest doGetUrlAsync(String url,
			RequestStatusListener requestListener) {
		NetworkRequest cd = new NetworkRequest(url, requestListener);
		Thread th = new Thread(cd);
		th.start();

		return cd;
	}

	public static NetworkRequest doGetUrlSync(String url) {
		if (!NetworkStatusReceiver.isConnected()) {
			return null;
		}
		NetworkRequest cd = new NetworkRequest(url, null);
		Thread th = new Thread(cd);
		th.start();

		// Time out: 100ms x 100 = 10 seconds
		for (int i = 0; i < 100; i++) {
			if (cd.isFinished()) {
				break;
			}

			try {
				Thread.sleep(200);
			} catch (Throwable t) {
			}
		}
		// Kill it now
		cd.abort();

		return cd;
	}

	public static NetworkRequest doPostUrlSync(String url, String content) {
		if (!NetworkStatusReceiver.isConnected()) {
			return null;
		}
		NetworkRequest cd = new NetworkRequest(url, content, null);
		Thread th = new Thread(cd);
		th.start();

		// Time out: 100ms x 100 = 10 seconds
		for (int i = 0; i < 100; i++) {
			if (cd.isFinished()) {
				break;
			}

			try {
				Thread.sleep(100);
			} catch (Throwable t) {
			}
		}
		// Kill it now
		cd.abort();
		th = null;

		return cd;
	}

	public static NetworkRequest doPostUrlSync(String url, Bitmap content) {
		NetworkRequest cd = new NetworkRequest(url, content, null);
		Thread th = new Thread(cd);
		th.start();

		// Time out: 100ms x 100 = 10 seconds
		for (int i = 0; i < 100; i++) {
			if (cd.isFinished()) {
				break;
			}

			try {
				Thread.sleep(100);
			} catch (Throwable t) {
			}
		}
		// Kill it now
		cd.abort();

		return cd;
	}

	public static NetworkRequest doPostUrlAsync(String url, String content,
			RequestStatusListener requestListener) {
		NetworkRequest cd = new NetworkRequest(url, content, requestListener);
		Thread th = new Thread(cd);
		th.start();

		return cd;
	}

	public static NetworkRequest doPostUrlAsync(String url, Bitmap content,
			RequestStatusListener requestListener) {
		NetworkRequest cd = new NetworkRequest(url, content, requestListener);
		Thread th = new Thread(cd);
		th.start();

		return cd;
	}

	public static ContentDownloader doDownloadContent(String url,
			String content, RequestStatusListener requestListener) {
		if (requestListener != null) {
			ContentDownloader cd = new ContentDownloader(url, content,
					requestListener, true);
			Thread th = new Thread(cd);
			th.start();
			return cd;
		}

		ContentDownloader cd = new ContentDownloader(url, content,
				requestListener, true);
		cd.run();
		return cd;
	}	
//	public static void finishSession() {
//		NetworkRequest cd = new NetworkRequest(null, null);
//		cd.finishSession();
//	}
	
//	public static boolean netWorkIsAvaiable() {
//		return mCurrentNetworkStatus != CONNECTION_TYPE_OFF;
//	}
}